package com.hw;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.dao.DepartmentDao;
import com.hw.dao.EmployeeDao;
import com.hw.dao.StudentDao;
import com.hw.dao.UserDao;
import com.hw.pojo.Department;
import com.hw.pojo.Employee;
import com.hw.pojo.Student;
import com.hw.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Demo01 {

    @Test
    public void testFindAll() throws Exception{

        //读取配置文件
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        //创建SqlSessionFactory工厂
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //使用工厂生产SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //-------------------------------------------------------------
        //使用SqlSession创建Dao接口的代理对象
        EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);

        PageHelper.startPage(1,2);
        //使用代理对象执行方法
        List<Employee> employees = employeeDao.findAll();

        PageInfo<Employee> userPageInfo = new PageInfo<Employee>(employees);

        System.out.println(userPageInfo);

        //释放资源
        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void testFindEmployeeById() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
        Employee employeeById = employeeDao.findEmployeeById(1);
        System.out.println(employeeById);
        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void testUpdateEmployeeById() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
        Employee employee = employeeDao.findEmployeeById(4);
        employee.setUsername("王五");
        employee.setSalary(2500.5);
        employee.setDeptId(1);
        int count = employeeDao.updateEmployeeById(employee);
        System.out.println(count);
        sqlSession.commit();
        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void testAddEmployee() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmployeeDao employeeDao = sqlSession.getMapper(EmployeeDao.class);
        Employee employee = new Employee();
        employee.setUsername("张四");
        employee.setSalary(3000.5);
        employee.setDeptId(1);
        System.out.println(employeeDao.addEmployee(employee));
        sqlSession.commit();
        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void departmentFindDeptById()throws Exception{


        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();


        DepartmentDao departmentDao = sqlSession.getMapper(DepartmentDao.class);

        Department department = departmentDao.findDepartmentById(1);

        System.out.println(department);

        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void studentFindById()throws Exception{

        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        StudentDao studentDao= sqlSession.getMapper(StudentDao.class);

        List<Student> studentList = studentDao.findStudentById(1);

        System.out.println(studentList);

        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void userFindAll()throws Exception{

        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        List<User> userList = userDao.findAll();

        System.out.println(userList);

        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void userFindByUser()throws Exception{

        InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserDao userDao = sqlSession.getMapper(UserDao.class);

        User user = new User();
        user.setUsername("lisi");
        User userList = userDao.findByUser(user);

        System.out.println(userList);

        sqlSession.close();
        inputStream.close();
    }

    @Test
    public void testUpdateUser()throws Exception {

        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(in);
        SqlSession session = factory.openSession();

        UserDao userDao = session.getMapper(UserDao.class);

        User user = new User();
        user.setId(1);
//        user.setUsername("李四");
        user.setPassword("456");

        userDao.updateByUser(user);

        session.commit();
        session.close();
        in.close();
    }
}
